package com.finger.orderservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutil.helper.JwtHelper;
import com.finger.modelservice.enums.OrderStatusEnum;
import com.finger.modelservice.model.order.OrderInfo;
import com.finger.modelservice.result.Result;
import com.finger.modelservice.vo.order.OrderQueryVo;
import com.finger.orderservice.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 此类接口用于前台网站
 */
@RestController
@RequestMapping("site/order/orderInfo")
public class FrontOrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    // 根据排班号和就诊人信息生成订单并返回订单id
    @PostMapping("auth/saveOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@PathVariable String patientId,
                            @PathVariable String scheduleId){

        String orderId = orderInfoService.saveOrder(scheduleId,patientId);

        return Result.ok(orderId);
    }

    // 前台用户 订单查找分页带条件
    @PostMapping("auth/findOrderInfoPage/{current}/{size}")
    public Result findOrderInfoPage(@RequestBody OrderQueryVo orderQueryVo,
                                    @PathVariable Integer current,
                                    @PathVariable Integer size,
                                    HttpServletRequest request){

        String userId = JwtHelper.getUserId(request.getHeader("token"));

        orderQueryVo.setUserId(userId);

        Page<OrderInfo> page = new Page<>(current, size);

        IPage<OrderInfo> result = orderInfoService.findOrderInfoPage(page,orderQueryVo);

        return Result.ok(result);
    }

    // 前台，获取订单状态集合
    @GetMapping("auth/getStatusList")
    public Result getStatusList(){

        return Result.ok(OrderStatusEnum.getStatusList());

    }

    // 根据订单Id获取订单信息
    @GetMapping("auth/getOrderById/{orderId}")
    public Result getOrderById(@PathVariable String orderId){

        OrderInfo orderInfo = orderInfoService.findOrderInfo(orderId);

        return Result.ok(orderInfo);
    }
}
